package com.driver.core.convert;

import com.driver.core.Column;
import com.driver.core.DBManager;
import com.driver.core.StringUtil;
import com.driver.core.Table;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySqlService extends AbstractSqlService {

    private MySqlService() {
    }

    public static MySqlService getInstance() {
        return MysqlConvertSingle.INSTANCE;
    }

    private static class MysqlConvertSingle {
        private static final MySqlService INSTANCE = new MySqlService();
    }

    @Override
    public String columnTypeToJavaType(String columnType) {
        if (columnType.contains("INT") || columnType.contains("int")) {
            return "Integer";
        } else if (columnType.equalsIgnoreCase("varchar") || columnType.equalsIgnoreCase("char") || columnType.equalsIgnoreCase("text")) {
            return "String";
        }else if (columnType.equalsIgnoreCase("bigint") ) {
            return "Long";
        }
        else if (columnType.equalsIgnoreCase("tinyint") ) {
            return "Byte";
        }
        else if (columnType.equalsIgnoreCase("smallint") ) {
            return "Integer";
        }
        else if (columnType.equalsIgnoreCase("datetime")) {
            return "Date";
        }
        else if (columnType.equalsIgnoreCase("TIMESTAMP")) {
            return "Timestamp";
        } else if (columnType.equalsIgnoreCase("decimal")) {
            return "BigDecimal";
        }
        else if (columnType.equalsIgnoreCase("float")) {
            return "Float";
        }
        else if (columnType.equalsIgnoreCase("bit")) {
            return "Boolean";
        }
        else if (columnType.equalsIgnoreCase("LONGTEXT")) {
            return "String";
        }
        throw new RuntimeException("未知的字段类型" + columnType + "无法转换");
    }

    @Override
    public List<Table> getTable() throws SQLException {
        return super.getTable(new ICallBack() {
            @Override
            public void getPrivateKey(DatabaseMetaData dbMetaData, List<Column> columns) throws SQLException {
                ResultSet tabs = dbMetaData.getTables(null, null, DBManager.getConfig().getTable(), new String[]{"TABLE"});
                ResultSet pkInfo = dbMetaData.getPrimaryKeys(null, "%",  DBManager.getConfig().getTable());
                while (pkInfo.next()) {
                    String pk_name = pkInfo.getString("PK_NAME");
                    System.out.println(pk_name);
                    if (pk_name.equalsIgnoreCase("PRIMARY")) {
                        String column_name = pkInfo.getString("COLUMN_NAME");
                        for (Column column : columns) {
                            if (column.getColumnName().equals(column_name)) {
                                column.setPrivateKey(true);
                            }
                        }
                    }
                }
            }
        });
    }
}
